import Vue from 'vue'
import Router from 'vue-router'

// 页面组件
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import Register from '../views/Register.vue'
import Students from '../views/Students.vue'
import Dormitories from '../views/Dormitories.vue'
import Allocation from '../views/Allocation.vue'
import WaterElectricityFee from '../views/WaterElectricityFee.vue'
import About from '../views/About.vue'
import Charts from '../views/Charts.vue' // 图表页面

Vue.use(Router)

const router = new Router({
  routes: [
    { path: '/', component: Home },
    { path: '/login', component: Login },
    { path: '/register', component: Register },
    { path: '/students', component: Students },
    { path: '/dormitories', component: Dormitories },
    { path: '/allocation', component: Allocation },
    { path: '/fees', component: WaterElectricityFee },
    { path: '/about', component: About },
    { path: '/charts', component: Charts }
  ]
})

// 全局路由守卫（权限控制）
router.beforeEach((to, from, next) => {
  const publicPages = ['/login', '/register']
  const authRequired = !publicPages.includes(to.path)
  const isLoggedIn = localStorage.getItem('isLoggedIn')

  if (authRequired && !isLoggedIn) {
    next('/login')
  } else {
    next()
  }
})

export default router